package com.ixuestar.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ixuestar.dao.IxuestarEasyLearnMapper;
import com.ixuestar.entity.MinxueStu;
import com.ixuestar.entity.School;
import com.ixuestar.entity.SchoolClass;
import com.ixuestar.entity.SchoolGrade;
import com.ixuestar.entity.SchoolStudent;
import com.ixuestar.entity.Temp;
import com.ixuestar.entity.YxIncludegrade;
import com.ixuestar.entity.YxKnowledge;
import com.ixuestar.entity.YxKnowledgevideo;
import com.ixuestar.entity.YxMembership;
import com.ixuestar.entity.YxPlayhistory;
import com.ixuestar.entity.YxSubKnowledge;
import com.ixuestar.entity.YxVideootherinfo;
import com.ixuestar.entity.YxWxuser;
import com.ixuestar.service.IxuestarEasyLearnService;
import com.ixuestar.wxutil.GetwxOpenId;

@Service("ixuestarEasyLearnService")
@Transactional
public class IxuestarEasyLearnServiceImpl implements IxuestarEasyLearnService{

	@Autowired
	private IxuestarEasyLearnMapper ixuestarEasyLearnMapper;
	
	public Logger logger = Logger.getLogger(IxuestarEasyLearnServiceImpl.class);
	
	@Override
	public JSONObject getWxuserInfo(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		int code = 0;
		JSONObject yxuserObject = new JSONObject();
		try {
			String wxIv = jsonObject.getString("wxIv");
			String openId = jsonObject.getString("openId");
			String wxSessionKey = jsonObject.getString("wxSessionKey");
			String encryptedData = jsonObject.getString("encryptedData");
			//执行接口
			final JSONObject wxTelephone = GetwxOpenId.getWxTelephone(encryptedData, wxSessionKey, wxIv);
			//创建用户信息
			if (!wxTelephone.isEmpty()) {
				String phoneNumber = wxTelephone.getString("phoneNumber");
				//首先判断是否已经存在此用户信息
				Map<String, Object> hashMap = new HashMap<>();
				hashMap.put("lineKey", "ywOpenId");
				hashMap.put("lineValue", openId);
				YxWxuser yxWxuser = ixuestarEasyLearnMapper.getYxWxuser(hashMap);
				//如果不存在 则写入
				if (yxWxuser==null) {
					YxWxuser yxWxuser2 = new YxWxuser();
					//创建ywId
					String ywId = "yw" + new Date().getTime() + (int)(Math.random()*10);
					//创建时间
					String ywCreatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
					yxWxuser2.setYwId(ywId);
					yxWxuser2.setYwOpenId(openId);
					yxWxuser2.setYwImgUrl("");
					yxWxuser2.setYwType("0");
					yxWxuser2.setYwTelephone(phoneNumber);
					yxWxuser2.setYwCreatetime(ywCreatetime);
					yxWxuser2.setIxssId(null);
					ixuestarEasyLearnMapper.writeYxWxuser(yxWxuser2);
					yxuserObject.putAll((JSONObject)JSONObject.toJSON(yxWxuser2));
				}else {
					//如果存在 则判断是否绑定
					String ixssId = yxWxuser.getIxssId();
					if (ixssId!=null) {
						code = 3;
						hashMap.put("lineKey", "ixssId");
						hashMap.put("lineValue", ixssId);
						SchoolStudent schoolStudent = ixuestarEasyLearnMapper.getSchoolStudents(hashMap).get(0);
						String ixssName = schoolStudent.getIxssName();
						yxuserObject.put("ixssId", ixssId);
						yxuserObject.put("ixssName", ixssName);
						String ixscId = schoolStudent.getIxscId();
						//获取班级信息
						hashMap.put("lineKey", "ixscId");
						hashMap.put("lineValue", ixscId);
						SchoolClass schoolClass = ixuestarEasyLearnMapper.getSchoolClasses(hashMap).get(0);
						String ixscName = schoolClass.getIxscName();
						yxuserObject.put("ixscId", ixscId);
						yxuserObject.put("ixscName", ixscName);
						String ixsgId = schoolClass.getIxsgId();
						//获取年级信息
						hashMap.put("lineKey", "ixsgId");
						hashMap.put("lineValue", ixsgId);
						SchoolGrade schoolGrade = ixuestarEasyLearnMapper.getSchoolGrades(hashMap).get(0);
						String ixsgName = schoolGrade.getIxsgName();
						yxuserObject.put("ixsgId", ixsgId);
						yxuserObject.put("ixsgName", ixsgName);
						String ixsId = schoolGrade.getIxsId();
						hashMap.put("lineKey", "ixsId");
						hashMap.put("lineValue", ixsId);
						School school = ixuestarEasyLearnMapper.getSchoolInfo(hashMap).get(0);
						String ixsName = school.getIxsName();
						yxuserObject.put("ixsName", ixsName);
					}else {						
						code = 2;
					}
					yxuserObject.putAll((JSONObject)JSONObject.toJSON(yxWxuser));
				}
			}else {
				code = 1;
			}
			//返回
			jsonObject.put("code", code);
			jsonObject.put("yxWxuser", yxuserObject);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	@Override
	public JSONObject getSchoolInfos() {
		// TODO Auto-generated method stub
		JSONObject jsonObject = new JSONObject();
		try {
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("flag", 0);
			List<School> schoolInfo = ixuestarEasyLearnMapper.getSchoolInfo(hashMap);
			int schoolNumber = schoolInfo.size();
			jsonObject.put("schoolList", schoolInfo);
			jsonObject.put("schoolNumber", schoolNumber);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject;
	}

	@Override
	public JSONObject getSchoolGradeList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		try {
			//接收参数
			String ixsId = jsonObject.getString("ixsId");
			//获取学校年级列表
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixsId");
			hashMap.put("lineValue", ixsId);
			List<SchoolGrade> schoolGrades = ixuestarEasyLearnMapper.getSchoolGrades(hashMap);
			int schoolGradeNumber = schoolGrades.size();
			jsObject.put("schoolGradesList", schoolGrades);
			jsObject.put("schoolGradeNumber", schoolGradeNumber);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}

	@Override
	public JSONObject getSchoolClassList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		try {
			JSONArray jsonArray = new JSONArray();
			//首先接收参数
			String ixsgId = jsonObject.getString("ixsgId");
			//获取班级列表信息
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixsgId");
			hashMap.put("lineValue", ixsgId);
			List<SchoolClass> schoolClasses = ixuestarEasyLearnMapper.getSchoolClasses(hashMap);
			for (int i = 0; i < schoolClasses.size(); i++) {
				SchoolClass schoolClass = schoolClasses.get(i);
				String ixscNo = schoolClass.getIxscNo();
				if (!(ixscNo.substring(ixscNo.length()-2).equals("00"))) {
					jsonArray.add(schoolClass);
				}
			}
			int schoolClassNumber = jsonArray.size();
			jsObject.put("schoolClassesList", jsonArray);
			jsObject.put("schoolClassNumber", schoolClassNumber);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}

	@Override
	public JSONObject getSchoolStudentList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		try {
			JSONArray jsonArray = new JSONArray();
			//首先接收参数
			String ixscId = jsonObject.getString("ixscId");
			//获取班级学生信息
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixscId");
			hashMap.put("lineValue", ixscId);
			List<SchoolStudent> schoolStudents = ixuestarEasyLearnMapper.getSchoolStudents(hashMap);
			for (int i = 0; i < schoolStudents.size(); i++) {
				SchoolStudent schoolStudent = schoolStudents.get(i);
				String ixssId = schoolStudent.getIxssId();
				//获取会员绑定信息
				hashMap.put("lineKey", "ixssId");
				hashMap.put("lineValue", ixssId);
				YxMembership membershipInfo = ixuestarEasyLearnMapper.getMembershipInfo(hashMap);
				if (membershipInfo!=null) {
					String ymId = membershipInfo.getYmId();
					//获取会员卡号
					JSONObject stuJson = (JSONObject)JSONObject.toJSON(schoolStudent);
					stuJson.put("ymId", ymId);
					jsonArray.add(stuJson);
				}
			}
			int schoolStudentsNumber = jsonArray.size();
			jsObject.put("schoolStudentsList", jsonArray);
			jsObject.put("schoolStudentsNumber", schoolStudentsNumber);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject bindingWxuserInfo(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		int code = 0;
		try {
			JSONObject yxuserObject = new JSONObject();
			String ywId = jsonObject.getString("ywId");
			String ixssId = jsonObject.getString("ixssId");
			//首先获取用户的微信信息
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ywId");
			hashMap.put("lineValue", ywId);
			YxWxuser yxWxuser = ixuestarEasyLearnMapper.getYxWxuser(hashMap);
			if (yxWxuser!=null) {
				yxWxuser.setIxssId(ixssId);
				ixuestarEasyLearnMapper.updateYxWxuser(yxWxuser);
				
				yxuserObject.putAll((JSONObject)JSONObject.toJSON(yxWxuser));
				
				hashMap.put("lineKey", "ixssId");
				hashMap.put("lineValue", ixssId);
				SchoolStudent schoolStudent = ixuestarEasyLearnMapper.getSchoolStudents(hashMap).get(0);
				String ixssName = schoolStudent.getIxssName();
				yxuserObject.put("ixssId", ixssId);
				yxuserObject.put("ixssName", ixssName);
				String ixscId = schoolStudent.getIxscId();
				//获取班级信息
				hashMap.put("lineKey", "ixscId");
				hashMap.put("lineValue", ixscId);
				SchoolClass schoolClass = ixuestarEasyLearnMapper.getSchoolClasses(hashMap).get(0);
				String ixscName = schoolClass.getIxscName();
				yxuserObject.put("ixscId", ixscId);
				yxuserObject.put("ixscName", ixscName);
				String ixsgId = schoolClass.getIxsgId();
				//获取年级信息
				hashMap.put("lineKey", "ixsgId");
				hashMap.put("lineValue", ixsgId);
				SchoolGrade schoolGrade = ixuestarEasyLearnMapper.getSchoolGrades(hashMap).get(0);
				String ixsgName = schoolGrade.getIxsgName();
				yxuserObject.put("ixsgId", ixsgId);
				yxuserObject.put("ixsgName", ixsgName);
				String ixsId = schoolGrade.getIxsId();
				hashMap.put("lineKey", "ixsId");
				hashMap.put("lineValue", ixsId);
				School school = ixuestarEasyLearnMapper.getSchoolInfo(hashMap).get(0);
				String ixsName = school.getIxsName();
				yxuserObject.put("ixsName", ixsName);
			}else {
				code = 1;
			}
			jsObject.put("code", code);
			jsObject.put("yxWxuser", yxuserObject);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject getSelectedList(JSONObject jsonObject,int code) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		int co = 0;
		try {
			String ysId = jsonObject.getString("ysId");
			String ixsgId = jsonObject.getString("ixsgId");
			//获取学生信息列表
			Map<String, Object> hashMap = new HashMap<>();
			//获取知识点关联年级
			JSONArray jsonArray = new JSONArray();
			hashMap.put("lineKey", "ixsgId");
			hashMap.put("lineValue", ixsgId);
			YxIncludegrade yxIncludegrade = ixuestarEasyLearnMapper.getYxIncludegrade(hashMap);
			logger.info("------yxIncludegrade--"+yxIncludegrade+"------------");
			if (yxIncludegrade!=null) {
				String ygId = yxIncludegrade.getYgId();
				//获取一级知识点列表
				hashMap.put("flag", 0);
				hashMap.put("ygId", ygId);
				hashMap.put("ysId", ysId);
				List<YxKnowledge> yxKnowledges = ixuestarEasyLearnMapper.getYxKnowledges(hashMap);
				logger.info("------yxKnowledges--"+yxKnowledges+"------------");
				for (int i = 0; i < yxKnowledges.size(); i++) {
					//获取当个一级知识点对象
					YxKnowledge yxKnowledge = yxKnowledges.get(i);
					String ykId = yxKnowledge.getYkId();
					//获取二级知识点列表	
					hashMap.put("flag", 0);
					hashMap.put("lineKey", "ykId");
					hashMap.put("lineValue", ykId);
					List<YxSubKnowledge> yxSubKnowledges = ixuestarEasyLearnMapper.getYxSubKnowledges(hashMap);
					logger.info("------yxSubKnowledges--"+yxSubKnowledges+"------------");
					for (int j = 0; j < yxSubKnowledges.size(); j++) {
						//获取二级知识点列表
						YxSubKnowledge yxSubKnowledge = yxSubKnowledges.get(j);
						String yskId = yxSubKnowledge.getYskId();
						hashMap.put("lineKey", "yskId");
						hashMap.put("lineValue", yskId);
						//获取播放量
						Temp playLikeNum = ixuestarEasyLearnMapper.getPlayLikeNum(hashMap);
						int likeNum=0,playNum=0;
						if (playLikeNum!=null) {
							likeNum = Integer.parseInt(playLikeNum.getA());
							playNum = Integer.parseInt(playLikeNum.getB());
						}

						JSONObject subKnowledge = (JSONObject)JSONObject.toJSON(yxSubKnowledge);
						subKnowledge.put("likeNum", likeNum);
						subKnowledge.put("playNum", playNum);
						jsonArray.add(subKnowledge);
					}
				}
				jsObject.put("recommendVideo", jsonArray);
				//获取精选
				hashMap.put("flag", 1);
				hashMap.put("ygId", ygId);
				hashMap.put("ysId", ysId);
				JSONArray jsonArray1 = new JSONArray();
				List<YxKnowledge> yxKnowledges1 = ixuestarEasyLearnMapper.getYxKnowledges(hashMap);
				logger.info("------yxKnowledges--"+yxKnowledges+"------------");
				for (int i = 0; i < yxKnowledges1.size(); i++) {
					//获取当个一级知识点对象
					YxKnowledge yxKnowledge = yxKnowledges1.get(i);
					String ykId = yxKnowledge.getYkId();
					//获取二级知识点列表
					hashMap.put("flag", 1);
					hashMap.put("lineKey", "ykId");
					hashMap.put("lineValue", ykId);
					List<YxSubKnowledge> yxSubKnowledges = ixuestarEasyLearnMapper.getYxSubKnowledges(hashMap);
					logger.info("------yxSubKnowledges--"+yxSubKnowledges+"------------");
					for (int j = 0; j < yxSubKnowledges.size(); j++) {
						//获取二级知识点列表
						YxSubKnowledge yxSubKnowledge = yxSubKnowledges.get(j);
						String yskId = yxSubKnowledge.getYskId();
						hashMap.put("lineKey", "yskId");
						hashMap.put("lineValue", yskId);
						//获取播放量
						Temp playLikeNum = ixuestarEasyLearnMapper.getPlayLikeNum(hashMap);
						int likeNum=0,playNum=0;
						if (playLikeNum!=null) {
							likeNum = Integer.parseInt(playLikeNum.getA());
							playNum = Integer.parseInt(playLikeNum.getB());
						}
						JSONObject subKnowledge = (JSONObject)JSONObject.toJSON(yxSubKnowledge);
						subKnowledge.put("likeNum", likeNum);
						subKnowledge.put("playNum", playNum);
						jsonArray1.add(subKnowledge);
					}
				}
				jsObject.put("selectedVideo", jsonArray1);
			}else {
				co = 3;
			}
			jsObject.put("code", co);
			logger.info("--------jsObject-"+jsObject+"------------");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject getVideoList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsonObject2 = new JSONObject();
		try {
			JSONArray jsonArray = new JSONArray();
			String ywId = jsonObject.getString("ywId");
			String yskId = jsonObject.getString("yskId");
			//请求视频列表
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "yskId");
			hashMap.put("lineValue", yskId);
			List<YxKnowledgevideo> yxKnowledgevideos = ixuestarEasyLearnMapper.getYxKnowledgevideos(hashMap);
			for (int i = 0; i < yxKnowledgevideos.size(); i++) {
				//获取列表参数
				YxKnowledgevideo yxKnowledgevideo = yxKnowledgevideos.get(i);
				String ykvId = yxKnowledgevideo.getYkvId();
				JSONObject videoObject = (JSONObject)JSONObject.toJSON(yxKnowledgevideo);
				//获取点赞数和播放数
				hashMap.put("lineKey", "ykvId");
				hashMap.put("lineValue", ykvId);
				YxVideootherinfo yxVideootherinfo = ixuestarEasyLearnMapper.getYxVideootherinfo(hashMap);
				Integer yvoLikes = 0,yvoPlays=0;
				if(yxVideootherinfo!=null) {
					yvoLikes = yxVideootherinfo.getYvoLikes();
					yvoPlays = yxVideootherinfo.getYvoPlays();
					videoObject.put("yvoLikes", yvoLikes);
					videoObject.put("yvoPlays", yvoPlays);
					//是否点赞
					hashMap.put("flag", 1);
					hashMap.put("ywId", ywId);
					hashMap.put("ykvId", ykvId);
					YxPlayhistory yxLikehistory = ixuestarEasyLearnMapper.getYxLikehistory(hashMap);
					if (yxLikehistory!=null) {
						videoObject.put("isLike", "Y");
					}else {
						videoObject.put("isLike", "N");
					}
					//是否收藏
					hashMap.put("flag", 1);
					hashMap.put("ywId", ywId);
					hashMap.put("ykvId", ykvId);
					YxPlayhistory yxCollecthistory = ixuestarEasyLearnMapper.getYxCollecthistory(hashMap);
					if (yxCollecthistory!=null) {
						videoObject.put("isCollect", "Y");
					}else {
						videoObject.put("isCollect", "N");
					}
					hashMap.put("ywId", ywId);
					hashMap.put("ykvId", ykvId);
					YxPlayhistory yxPlayhistory = ixuestarEasyLearnMapper.getYxPlayhistory(hashMap);
					if (yxPlayhistory!=null) {
						String yphPlayRate = yxPlayhistory.getYphPlayRate();
						videoObject.put("isPlay", "Y");
						videoObject.put("yphPlayRate", yphPlayRate);
					}else {
						videoObject.put("isPlay", "N");
						videoObject.put("yphPlayRate", "00:00");
					}
				}else {
					videoObject.put("yvoLikes", yvoLikes);
					videoObject.put("yvoPlays", yvoPlays);
					videoObject.put("isLike", "N");
					videoObject.put("isPlay", "N");
					videoObject.put("yphPlayRate", "00:00");
				}
				jsonArray.add(videoObject);
			}
			//返回参数
			jsonObject2.put("videoList", jsonArray);
			jsonObject2.put("videoNume", jsonArray.size());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject2;
	}
	
	@Override
	public JSONObject writePlayhistory(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = null;
		try {
			String ywId = jsonObject.getString("ywId");
			String ykvId = jsonObject.getString("ykvId");
			//写入播放历史
			//首先查看是否存在播放列表
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("ywId", ywId);
			hashMap.put("ykvId", ykvId);
			YxPlayhistory yxPlayhistory = ixuestarEasyLearnMapper.getYxPlayhistory(hashMap);
			//首先查询是否存在播放历史
			//如果存在则更新
			if (yxPlayhistory!=null) {
				String yphCreatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
				yxPlayhistory.setYphPlayTime(yphCreatetime);
				ixuestarEasyLearnMapper.updatePlayHistory(yxPlayhistory);
			}else {
				//如果不存在,则写入
				YxPlayhistory yxPlayhistory2 = new YxPlayhistory();
				//生成id
				String yphId = "yph" + new Date().getTime()+(int)(Math.random()*10);
				String yphPlayTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
				yxPlayhistory2.setYphId(yphId);
				yxPlayhistory2.setYwId(ywId);
				yxPlayhistory2.setYkvId(ykvId);
				yxPlayhistory2.setYphPlayTime(yphPlayTime);
				ixuestarEasyLearnMapper.writePlayHistory(yxPlayhistory2);
				yxPlayhistory = yxPlayhistory2;
			}
			//写入播放量
			//首先查询此视频是否被播放过
			hashMap.put("lineKey", "ykvId");
			hashMap.put("lineValue", ykvId);
			YxVideootherinfo yxVideootherinfo = ixuestarEasyLearnMapper.getYxVideootherinfo(hashMap);
			//如果没有播放过 创建
			if (yxVideootherinfo==null) {
				YxVideootherinfo yxVideootherinfo2 = new YxVideootherinfo();
				//创建id
				String yvoId = "yvo" + new Date().getTime() + (int)(Math.random()*10);
				yxVideootherinfo2.setYvoId(yvoId);
				yxVideootherinfo2.setYvoLikes(0);
				yxVideootherinfo2.setYvoPlays(1);
				yxVideootherinfo2.setYkvId(ykvId);
				ixuestarEasyLearnMapper.writeYxVideootherinfo(yxVideootherinfo2);
				yxVideootherinfo = yxVideootherinfo2;
			}else {
				//如果播放过 则更新
				Integer yvoPlays = yxVideootherinfo.getYvoPlays()+1;
				yxVideootherinfo.setYvoPlays(yvoPlays);
				ixuestarEasyLearnMapper.updateYxVideootherinfo(yxVideootherinfo);
			}
			
			jsObject = (JSONObject)JSONObject.toJSON(yxPlayhistory);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject writePlayRate(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsonObject2 = null;
		try {
			String ywId = jsonObject.getString("ywId");
			String ykvId = jsonObject.getString("ykvId");
			String yphPlayRate = jsonObject.getString("yphPlayRate");
			//请求
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("ywId", ywId);
			hashMap.put("ykvId", ykvId);
			YxPlayhistory yxPlayhistory = ixuestarEasyLearnMapper.getYxPlayhistory(hashMap);
			if (yxPlayhistory!=null) {
				yxPlayhistory.setYphPlayRate(yphPlayRate);
				ixuestarEasyLearnMapper.updatePlayHistory(yxPlayhistory);
			}
			jsonObject2 = (JSONObject)JSONObject.toJSON(yxPlayhistory);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject2;
	}
	
	@Override
	public void writeLikehistory(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		try {
			//获取参数
			String ywId = jsonObject.getString("ywId");
			String ykvId = jsonObject.getString("ykvId");
			Integer ylStatus = jsonObject.getInteger("ylStatus");
			if (ylStatus==1) {
				//获取是否点赞
				Map<String, Object> hashMap = new HashMap<>();
				hashMap.put("flag", 2);
				hashMap.put("ywId", ywId);
				hashMap.put("ykvId", ykvId);
				YxPlayhistory yxLikehistory = ixuestarEasyLearnMapper.getYxLikehistory(hashMap);
				//如果没有播放过 创建
				if (yxLikehistory==null) {
					YxPlayhistory yxLikehistory2 = new YxPlayhistory();
					//创建id
					String ylhId = "ylh" + new Date().getTime() + (int)(Math.random()*10);
					String yxLikeTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
					yxLikehistory2.setYphId(ylhId);
					yxLikehistory2.setYwId(ywId);
					yxLikehistory2.setYkvId(ykvId);
					yxLikehistory2.setYphStatus("Y");
					yxLikehistory2.setYphPlayTime(yxLikeTime);
					ixuestarEasyLearnMapper.writeLikeHistory(yxLikehistory2);
				}else {
					String yphStatus = yxLikehistory.getYphStatus();
					if (yphStatus.equals("N")) {
						yxLikehistory.setYphStatus("Y");
						yxLikehistory.setYphPlayTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ixuestarEasyLearnMapper.updateLikeHistory(yxLikehistory);
					}
				}
				//写入数据
				hashMap.put("lineKey", "ykvId");
				hashMap.put("lineValue", ykvId);
				YxVideootherinfo yxVideootherinfo = ixuestarEasyLearnMapper.getYxVideootherinfo(hashMap);
				if (yxVideootherinfo!=null) {
					Integer integer = yxVideootherinfo.getYvoLikes()+1;
					yxVideootherinfo.setYvoLikes(integer);
					ixuestarEasyLearnMapper.updateYxVideootherinfo(yxVideootherinfo);
				}else {
					YxVideootherinfo yxVideootherinfo2 = new YxVideootherinfo();
					//创建id
					String yvoId = "yvo" + new Date().getTime() + (int)(Math.random()*10);
					yxVideootherinfo2.setYvoId(yvoId);
					yxVideootherinfo2.setYvoLikes(1);
					yxVideootherinfo2.setYvoPlays(0);
					yxVideootherinfo2.setYkvId(ykvId);
					ixuestarEasyLearnMapper.writeYxVideootherinfo(yxVideootherinfo2);
				}
			}else if (ylStatus==-1) {
				//获取是否点赞
				Map<String, Object> hashMap = new HashMap<>();
				hashMap.put("flag", 2);
				hashMap.put("ywId", ywId);
				hashMap.put("ykvId", ykvId);
				YxPlayhistory yxLikehistory = ixuestarEasyLearnMapper.getYxLikehistory(hashMap);
				if (yxLikehistory!=null) {
					String yphStatus = yxLikehistory.getYphStatus();
					if (yphStatus.equals("Y")) {
						yxLikehistory.setYphStatus("N");
						yxLikehistory.setYphPlayTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ixuestarEasyLearnMapper.updateLikeHistory(yxLikehistory);
					}
				}
				//写入数据
				hashMap.put("lineKey", "ykvId");
				hashMap.put("lineValue", ykvId);
				YxVideootherinfo yxVideootherinfo = ixuestarEasyLearnMapper.getYxVideootherinfo(hashMap);
				if (yxVideootherinfo!=null) {
					Integer integer = yxVideootherinfo.getYvoLikes()-1;
					yxVideootherinfo.setYvoLikes(integer);
					ixuestarEasyLearnMapper.updateYxVideootherinfo(yxVideootherinfo);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	@Override
	public JSONObject writeCollectHistory(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsonObject2 = new JSONObject();
		try {
			//获取参数
			String ywId = jsonObject.getString("ywId");
			String ykvId = jsonObject.getString("ykvId");
			Integer ylStatus = jsonObject.getInteger("ylStatus");
			if (ylStatus==1) {
				//获取是否点赞
				Map<String, Object> hashMap = new HashMap<>();
				hashMap.put("flag", 2);
				hashMap.put("ywId", ywId);
				hashMap.put("ykvId", ykvId);
				YxPlayhistory yxCollecthistory = ixuestarEasyLearnMapper.getYxCollecthistory(hashMap);
				//如果没有播放过 创建
				if (yxCollecthistory==null) {
					YxPlayhistory yxCollecthistory2 = new YxPlayhistory();
					//创建id
					String ylhId = "ylh" + new Date().getTime() + (int)(Math.random()*10);
					String yxLikeTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
					yxCollecthistory2.setYphId(ylhId);
					yxCollecthistory2.setYwId(ywId);
					yxCollecthistory2.setYkvId(ykvId);
					yxCollecthistory2.setYphStatus("Y");
					yxCollecthistory2.setYphPlayTime(yxLikeTime);
					ixuestarEasyLearnMapper.writeCollectHistory(yxCollecthistory2);
				}else {
					String yphStatus = yxCollecthistory.getYphStatus();
					if (yphStatus.equals("N")) {
						yxCollecthistory.setYphStatus("Y");
						yxCollecthistory.setYphPlayTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ixuestarEasyLearnMapper.updateCollectHistory(yxCollecthistory);
					}
				}
//				//写入数据
//				hashMap.put("lineKey", "ykvId");
//				hashMap.put("lineValue", ykvId);
//				YxVideootherinfo yxVideootherinfo = ixuestarEasyLearnMapper.getYxVideootherinfo(hashMap);
//				if (yxVideootherinfo!=null) {
//					Integer integer = yxVideootherinfo.getYvoLikes()+1;
//					yxVideootherinfo.setYvoLikes(integer);
//					ixuestarEasyLearnMapper.updateYxVideootherinfo(yxVideootherinfo);
//				}else {
//					YxVideootherinfo yxVideootherinfo2 = new YxVideootherinfo();
//					//创建id
//					String yvoId = "yvo" + new Date().getTime() + (int)(Math.random()*10);
//					yxVideootherinfo2.setYvoId(yvoId);
//					yxVideootherinfo2.setYvoLikes(1);
//					yxVideootherinfo2.setYvoPlays(0);
//					yxVideootherinfo2.setYkvId(ykvId);
//					ixuestarEasyLearnMapper.writeYxVideootherinfo(yxVideootherinfo2);
//				}
			}else if (ylStatus==-1) {
				//获取是否点赞
				Map<String, Object> hashMap = new HashMap<>();
				hashMap.put("flag", 2);
				hashMap.put("ywId", ywId);
				hashMap.put("ykvId", ykvId);
				YxPlayhistory yxCollecthistory = ixuestarEasyLearnMapper.getYxCollecthistory(hashMap);
				if (yxCollecthistory!=null) {
					String yphStatus = yxCollecthistory.getYphStatus();
					if (yphStatus.equals("Y")) {
						yxCollecthistory.setYphStatus("N");
						yxCollecthistory.setYphPlayTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ixuestarEasyLearnMapper.updateCollectHistory(yxCollecthistory);
					}
				}
//				//写入数据
//				hashMap.put("lineKey", "ykvId");
//				hashMap.put("lineValue", ykvId);
//				YxVideootherinfo yxVideootherinfo = ixuestarEasyLearnMapper.getYxVideootherinfo(hashMap);
//				if (yxVideootherinfo!=null) {
//					Integer integer = yxVideootherinfo.getYvoLikes()-1;
//					yxVideootherinfo.setYvoLikes(integer);
//					ixuestarEasyLearnMapper.updateYxVideootherinfo(yxVideootherinfo);
//				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject2;
	}
	
	@Override
	public JSONObject getCollectVideosList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		try {
			JSONArray jsonArray = new JSONArray();
			String ywId = jsonObject.getString("ywId");
			//获取播放视频列表
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ywId");
			hashMap.put("lineValue", ywId);
			List<YxPlayhistory> yxCollecthistoriesList = ixuestarEasyLearnMapper.getYxCollecthistoriesList(hashMap);
			for (int i = 0; i < yxCollecthistoriesList.size(); i++) {
				//获取单个观看对象
				YxPlayhistory yxCollecthistory = yxCollecthistoriesList.get(i);
				//获取视频id
				String ykvId = yxCollecthistory.getYkvId();
				
				//获取单个视频信息
				hashMap.put("lineKey", "ykvId");
				hashMap.put("lineValue", ykvId);
				YxKnowledgevideo yxKnowledgevideo = ixuestarEasyLearnMapper.getYxKnowledgevideos(hashMap).get(0);
				
				JSONObject videoObject = (JSONObject)JSONObject.toJSON(yxKnowledgevideo);
				if (yxCollecthistory!=null) {
					videoObject.put("isCollect", "Y");
				}
				//是否点赞
				hashMap.put("flag", 1);
				hashMap.put("ywId", ywId);
				hashMap.put("ykvId", ykvId);
				YxPlayhistory yxLikehistory = ixuestarEasyLearnMapper.getYxLikehistory(hashMap);
				if (yxLikehistory!=null) {
					videoObject.put("isLike", "Y");
				}else {
					videoObject.put("isLike", "N");
				}
				hashMap.put("ywId", ywId);
				hashMap.put("ykvId", ykvId);
				YxPlayhistory yxPlayhistory = ixuestarEasyLearnMapper.getYxPlayhistory(hashMap);
				if (yxPlayhistory!=null) {
					String yphPlayRate = yxPlayhistory.getYphPlayRate();
					videoObject.put("isPlay", "Y");
					videoObject.put("yphPlayRate", yphPlayRate);
				}else {
					videoObject.put("isPlay", "N");
					videoObject.put("yphPlayRate", "00:00");
				}
				jsonArray.add(videoObject);
			}
			jsObject.put("yxKnowledgevideo", jsonArray);
			jsObject.put("yxKnowledgevideoNum", jsonArray.size());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject getPlayVideosList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		try {
			JSONArray jsonArray = new JSONArray();
			String ywId = jsonObject.getString("ywId");
			//获取播放视频列表
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ywId");
			hashMap.put("lineValue", ywId);
			List<YxPlayhistory> yxPlayhistoriesList = ixuestarEasyLearnMapper.getYxPlayhistoriesList(hashMap);
			for (int i = 0; i < yxPlayhistoriesList.size(); i++) {
				//获取单个观看对象
				YxPlayhistory yxPlayhistory = yxPlayhistoriesList.get(i);
				//获取视频id
				String ykvId = yxPlayhistory.getYkvId();
				//获取单个视频信息
				hashMap.put("lineKey", "ykvId");
				hashMap.put("lineValue", ykvId);
				YxKnowledgevideo yxKnowledgevideo = ixuestarEasyLearnMapper.getYxKnowledgevideos(hashMap).get(0);
				
				JSONObject videoObject = (JSONObject)JSONObject.toJSON(yxKnowledgevideo);
				if (yxPlayhistory!=null) {
					String yphPlayRate = yxPlayhistory.getYphPlayRate();
					videoObject.put("isPlay", "Y");
					videoObject.put("yphPlayRate", yphPlayRate);
				}
				//是否点赞
				hashMap.put("flag", 1);
				hashMap.put("ywId", ywId);
				hashMap.put("ykvId", ykvId);
				YxPlayhistory yxLikehistory = ixuestarEasyLearnMapper.getYxLikehistory(hashMap);
				if (yxLikehistory!=null) {
					videoObject.put("isLike", "Y");
				}else {
					videoObject.put("isLike", "N");
				}
				hashMap.put("flag", 1);
				hashMap.put("ywId", ywId);
				hashMap.put("ykvId", ykvId);
				YxPlayhistory yxCollecthistory = ixuestarEasyLearnMapper.getYxCollecthistory(hashMap);
				if (yxCollecthistory!=null) {
					videoObject.put("isCollect", "Y");
				}else {
					videoObject.put("isCollect", "N");
				}
				jsonArray.add(videoObject);
			}
			jsObject.put("yxKnowledgevideo", jsonArray);
			jsObject.put("yxKnowledgevideoNum", jsonArray.size());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject getLoginYueyixue(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		int code = 0;
		JSONObject jsObject = new JSONObject();
		try {
			//获取用户名
			String userName = jsonObject.getString("userName");
			String passWord = jsonObject.getString("passWord");
			//获取会员信息
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ymNo");
			hashMap.put("lineValue", userName);
			YxMembership yxMembership = ixuestarEasyLearnMapper.getYxMembership(hashMap);
			if (yxMembership!=null) {
				String ymPassWord = yxMembership.getYmPassWord();
				if(passWord.equals(ymPassWord)) {
					String ymId = yxMembership.getYmId();
					String ixssId = yxMembership.getIxssId();
					hashMap.put("lineKey", "ixssId");
					hashMap.put("lineValue", ixssId);
					MinxueStu minxueStuInfo = ixuestarEasyLearnMapper.getMinxueStuInfo(hashMap);
					if (minxueStuInfo!=null) {
						JSONObject json = (JSONObject)JSONObject.toJSON(minxueStuInfo);
						jsObject.putAll(json);
						jsObject.put("ixmId", ymId);
					}else {
						code = 2;
					}
				}else {
					code = 3;
				}
			}else {
				code = 1;
			}
			jsObject.put("code", code);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject getPlayVideo(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		try {
			Map<String, Object> hashMap = new HashMap<>();
			//获取推荐好课
			hashMap.put("flag", 0);
			JSONArray jsonArray1 = new JSONArray();
			List<YxKnowledge> yxKnowledgeList0 = ixuestarEasyLearnMapper.getYxKnowledgeLists(hashMap);
			for (int i = 0; i < yxKnowledgeList0.size(); i++) {
				YxKnowledge yxKnowledge = yxKnowledgeList0.get(i);
				//获取二级知识点
				String ykId = yxKnowledge.getYkId();
				//获取二级知识点
				hashMap.put("flag", 0);
				hashMap.put("lineKey", "ykId");
				hashMap.put("lineValue", ykId);
				List<YxSubKnowledge> yxSubKnowledges = ixuestarEasyLearnMapper.getYxSubKnowledges(hashMap);
				for (int j = 0; j < yxSubKnowledges.size(); j++) {
					YxSubKnowledge yxSubKnowledge = yxSubKnowledges.get(j);
					//获取二级
					String yskId = yxSubKnowledge.getYskId();
					//获取视频
					hashMap.put("lineKey", "yskId");
					hashMap.put("lineValue", yskId);
					Temp playLikeNum = ixuestarEasyLearnMapper.getPlayLikeNum(hashMap);
					int likeNum=0,playNum=0;
					if (playLikeNum!=null) {
						likeNum = Integer.parseInt(playLikeNum.getA());
						playNum = Integer.parseInt(playLikeNum.getB());
					}
					//转换jsoN
					JSONObject subKnowledge = (JSONObject)JSONObject.toJSON(yxSubKnowledge);
					subKnowledge.put("likeNum", likeNum);
					subKnowledge.put("playNum", playNum);
					jsonArray1.add(subKnowledge);
				}
			}
			jsObject.put("recommendVideo", jsonArray1);
			//获取精选特辑
			hashMap.put("flag", 1);
			JSONArray jsonArray2 = new JSONArray();
			List<YxKnowledge> yxKnowledgeList1 = ixuestarEasyLearnMapper.getYxKnowledgeLists(hashMap);
			for (int i = 0; i < yxKnowledgeList1.size(); i++) {
				YxKnowledge yxKnowledge = yxKnowledgeList1.get(i);
				//获取二级知识点
				String ykId = yxKnowledge.getYkId();
				//获取二级知识点
				hashMap.put("flag", 1);
				hashMap.put("lineKey", "ykId");
				hashMap.put("lineValue", ykId);
				List<YxSubKnowledge> yxSubKnowledges = ixuestarEasyLearnMapper.getYxSubKnowledges(hashMap);
				for (int j = 0; j < yxSubKnowledges.size(); j++) {
					YxSubKnowledge yxSubKnowledge = yxSubKnowledges.get(j);
					//获取二级
					String yskId = yxSubKnowledge.getYskId();
					//获取视频
					hashMap.put("lineKey", "yskId");
					hashMap.put("lineValue", yskId);
					Temp playLikeNum = ixuestarEasyLearnMapper.getPlayLikeNum(hashMap);
					int likeNume=0,playNume=0;
					if (playLikeNum!=null) {
						likeNume = Integer.parseInt(playLikeNum.getA());
						playNume = Integer.parseInt(playLikeNum.getB());
					}
					//转换
					JSONObject subKnowledge = (JSONObject)JSONObject.toJSON(yxSubKnowledge);
					subKnowledge.put("likeNum", likeNume);
					subKnowledge.put("playNum", playNume);
					jsonArray2.add(subKnowledge);
				}
			}
			jsObject.put("selectedVideo", jsonArray2);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public void updateLoginPassWord(String userName, String passWord) {
		// TODO Auto-generated method stub
		try {
			YxMembership yxMembership = new YxMembership();
			yxMembership.setYmNo(userName);
			yxMembership.setYmPassWord(passWord);
			ixuestarEasyLearnMapper.updateYxMembership(yxMembership);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
